#include "widget.h"
#include "ui_widget.h"
#include <QRegularExpressionValidator>
#include <QString>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    ui->lineEdit_name->setClearButtonEnabled(true);
    ui->lineEdit_password->setClearButtonEnabled(true);
    ui->lineEdit_password_again->setClearButtonEnabled(true);
    ui->lineEdit_phone->setClearButtonEnabled(true);
    ui->radioButton_male->setChecked(true);
    ui->lineEdit_name->setPlaceholderText("请输入姓名");
    ui->lineEdit_phone->setPlaceholderText("请输入电话号码");
    ui->lineEdit_password->setPlaceholderText("请输入密码");
    ui->lineEdit_password->setEchoMode(QLineEdit::Password);
    ui->lineEdit_password_again->setPlaceholderText("请再次输入密码");
    ui->lineEdit_password_again->setEchoMode(QLineEdit::Password);

    QRegularExpression regExp("^1\\d{10}$");
    ui->lineEdit_phone->setValidator(new QRegularExpressionValidator(regExp));
    for(int i = 0; i < 4; i++)
        allPass |= (1 << i);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_clicked()
{
    QString gender = ui->radioButton_male->isChecked() ? "男" : "女";
    qDebug() << "姓名：" << ui->lineEdit_name->text();
    qDebug() << "密码：" << ui->lineEdit_password->text();
    qDebug() << "性别：" << gender;
    qDebug() << "电话号码：" << ui->lineEdit_phone->text();
}


void Widget::on_lineEdit_phone_textEdited(const QString &text)
{
    QString content = text;
    int pos = 0;
    if(ui->lineEdit_phone->validator()->validate(content, pos) == QValidator::Acceptable)
        passButton |= 1;
    else passButton &= (allPass ^ 1);
    ui->pushButton->setEnabled(passButton == allPass);
}


void Widget::on_lineEdit_password_textEdited(const QString &password)
{
    int check = (password.size() >= 8);
    for(auto& c : password)
        if(c >= '0' && c <= '9')
        {
            check++;
            break;
        }
    for(auto& c : password)
        if(c >= 'a' && c <= 'z')
        {
            check++;
            break;
        }
    for(auto& c : password)
        if(c >= 'A' && c <= 'Z')
        {
            check++;
            break;
        }
    if(check == 4) passButton |= 2;
    else passButton &= (allPass ^ 2);
    ui->pushButton->setEnabled(passButton == allPass);
}


void Widget::on_lineEdit_name_textEdited(const QString &name)
{
    if(name.size()) passButton |= 4;
    else passButton &= (allPass ^ 4);
    ui->pushButton->setEnabled(passButton == allPass);
}


void Widget::on_lineEdit_password_again_textEdited(const QString &password)
{
    if(password == ui->lineEdit_password->text()) passButton |= 8;
    else passButton &= (allPass ^ 8);
    ui->pushButton->setEnabled(passButton == allPass);
}


void Widget::on_checkBox_toggled(bool checked)
{
    if(checked){
        ui->lineEdit_password->setEchoMode(QLineEdit::Normal);
        ui->lineEdit_password_again->setEchoMode(QLineEdit::Normal);
    } else {
        ui->lineEdit_password->setEchoMode(QLineEdit::Password);
        ui->lineEdit_password_again->setEchoMode(QLineEdit::Password);
    }
}

